// FagForm.cpp : implementation file
//

#include "stdafx.h"
#include "EBASApp.h"
#include "FagForm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFagForm

IMPLEMENT_DYNCREATE(CFagForm, CFormView)

CFagForm::CFagForm()
	: CFormView(CFagForm::IDD)
{
	//{{AFX_DATA_INIT(CFagForm)
	m_navn = _T("");
	m_guid = _T("");
	//}}AFX_DATA_INIT

	m_pFag = NULL;
}

CFagForm::~CFagForm()
{
}

void CFagForm::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFagForm)
	DDX_Control(pDX, IDC_COMBO_KARAKTERSKALA, m_skala);
	DDX_Control(pDX, IDC_COMBO_BOGPAKKE, m_bogpakke);
	DDX_Control(pDX, IDC_LIST_FAG, m_list);
	DDX_Text(pDX, IDC_EDIT_NAVN, m_navn);
	DDX_Text(pDX, IDC_EDIT_GUID, m_guid);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFagForm, CFormView)
	//{{AFX_MSG_MAP(CFagForm)
	ON_LBN_DBLCLK(IDC_LIST_FAG, OnDblclkListFag)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFagForm diagnostics

#ifdef _DEBUG
void CFagForm::AssertValid() const
{
	CFormView::AssertValid();
}

void CFagForm::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFagForm message handlers

void CFagForm::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	IUnknown * pUnk = NULL; 
	ULONG fetched = 0;

	try {
		
		m_pEnumFag.CreateInstance(__uuidof(EBASDLLLib::EnumFag));
		m_pEnumFag->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IFagPtr pFag = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IFag), (void**)&pFag);
			pUnk = NULL;

			m_list.AddString(pFag->Navn);
			pFag = NULL;

			m_pEnumFag->Next(&pUnk, &fetched);
		}

		pUnk = NULL; fetched = 0;
		m_pEnumBogpakke.CreateInstance(__uuidof(EBASDLLLib::EnumBogpakke));
		m_pEnumBogpakke->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IBogpakkePtr pBogpakke = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IBogpakke), (void**)&pBogpakke);
			pUnk = NULL;

			m_bogpakke.AddString(pBogpakke->Navn);
			pBogpakke = NULL;

			m_pEnumBogpakke->Next(&pUnk, &fetched);
		}

		pUnk = NULL; fetched = 0;
		m_pEnumKarakterSkala.CreateInstance(__uuidof(EBASDLLLib::EnumKarakterSkala));
		m_pEnumKarakterSkala->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IKarakterSkalaPtr pKarakterSkala = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IKarakterSkala), (void**)&pKarakterSkala);
			pUnk = NULL;

			m_skala.AddString(pKarakterSkala->Navn);
			pKarakterSkala = NULL;

			m_pEnumKarakterSkala->Next(&pUnk, &fetched);
		}

	} catch (...) {

	}

	UpdateData(FALSE);
}

void CFagForm::UpdateList()
{
	IUnknown * pUnk = NULL; 
	ULONG fetched = 0;

	try {
		m_list.ResetContent();
		m_pEnumFag.CreateInstance(__uuidof(EBASDLLLib::EnumFag));
		m_pEnumFag->Next(&pUnk, &fetched);
		while (NULL != pUnk) {
			EBASDLLLib::IFagPtr pFag = NULL;
			pUnk->QueryInterface(__uuidof(EBASDLLLib::IFag), (void**)&pFag);
			pUnk = NULL;

			m_list.AddString(pFag->Navn);
			pFag = NULL;

			m_pEnumFag->Next(&pUnk, &fetched);
		}

	} catch (...) {

	}

}

void CFagForm::OnGemFag()
{
	UpdateData();

	try {
		EBASDLLLib::IFagPtr pFag = NULL;
		
		if (NULL == m_pFag)
			pFag = CEBASAppApp::gpEbasSession->OpretFag();
		else 
			pFag = m_pFag;

		int sel = -1;
		CString str;
		
		pFag->Navn = bstr_t(m_navn);

		sel = m_bogpakke.GetCurSel();
		m_bogpakke.GetLBText(sel, str);
		EBASDLLLib::IBogpakkePtr pBogpakke = NULL;
		pBogpakke = m_pEnumBogpakke->findBogpakkeNavn(bstr_t(str));
		
		pFag->BogPakke = pBogpakke;

		sel = m_skala.GetCurSel();
		m_skala.GetLBText(sel, str);
		EBASDLLLib::IKarakterSkalaPtr pKarakterSkala = NULL;
		pKarakterSkala = m_pEnumKarakterSkala->findKarakterSkalaNavn(bstr_t(str));
		pFag->KarakterSkala = pKarakterSkala;

		pFag->SetInfo();

		m_pFag = pFag;
		m_guid = (char*)m_pFag->GUID;

		UpdateList();
	} catch (...) {

	}

	UpdateData(FALSE);
}

void CFagForm::OnRetFag()
{
	try {
		CString oldName = m_navn;

		UpdateData();
		
		if (NULL == m_pFag)
			return;

		int sel = -1;
		CString str;
		
		m_pFag->Navn = bstr_t(m_navn);

		sel = m_bogpakke.GetCurSel();
		m_bogpakke.GetLBText(sel, str);
		EBASDLLLib::IBogpakkePtr pBogpakke = NULL;
		pBogpakke = m_pEnumBogpakke->findBogpakkeNavn(bstr_t(str));
		m_pFag->BogPakke = pBogpakke;

		sel = m_skala.GetCurSel();
		m_skala.GetLBText(sel, str);
		EBASDLLLib::IKarakterSkalaPtr pKarakterSkala = NULL;
		pKarakterSkala = m_pEnumFag->findFagNavn(bstr_t(str));
		m_pFag->KarakterSkala = pKarakterSkala;

		m_pFag->SetInfo();

		UpdateList();
/*
		sel = m_list.FindString(-1, oldName);
		m_list.DeleteString(sel);
		m_list.AddString(m_navn);
*/
	} catch (...) {

	}

	UpdateData(FALSE);
}

void CFagForm::OnSletFag()
{
	UpdateData();

	try {
		if (NULL == m_pFag)
			return;		
		
		m_pFag->Slet();
		m_pFag = NULL;

		m_guid = "";
		m_navn = "";
		m_bogpakke.SetCurSel(-1);
		m_skala.SetCurSel(-1);

		UpdateList();
	} catch (...) {

	}

	UpdateData(FALSE);
}

void CFagForm::OnDblclkListFag() 
{
	UpdateData();

	try {
		OnRyd();

		int pos = -1;
		CString str;

		pos = m_list.GetCurSel();
		m_list.GetText(pos, str);

		m_pFag = m_pEnumFag->findFagNavn(bstr_t(str));
		if (NULL == m_pFag)
			return;

		m_guid = (char*)m_pFag->GUID;
		m_navn = (char*)m_pFag->Navn;

		EBASDLLLib::IBogpakkePtr pBogpakke = NULL;
		pBogpakke = m_pFag->BogPakke;

		if (NULL != pBogpakke) {
			pos = m_bogpakke.FindString(-1, (char*)pBogpakke->Navn);
			m_bogpakke.SetCurSel(pos);
		}

		EBASDLLLib::IKarakterSkalaPtr pSkala = NULL;
		pSkala = m_pFag->KarakterSkala;
		if (NULL != pSkala) {
			pos = m_skala.FindString(-1, (char*)pSkala->Navn);
			m_skala.SetCurSel(pos);
		}
	} catch (...) {

	}

	UpdateData(FALSE);
}

void CFagForm::OnRyd()
{
	UpdateData();

	try {
		m_pFag = NULL;

		m_guid = "";
		m_navn = "";
		m_bogpakke.SetCurSel(-1);
		m_skala.SetCurSel(-1);
	} catch (...) {

	}

	UpdateData(FALSE);
}